package com.zjl.学习.第05章_作业控制.A_作业的启动;

import com.zjl.springbatch.util.OutColour;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.scope.context.StepContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@EnableBatchProcessing
@Configuration
public class A3_Config {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Tasklet tasklet005(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                StepContext stepContext = chunkContext.getStepContext();//获取 Job的 入参，配置 执行者等
                Map<String, Object> jobParameters = stepContext.getJobParameters();//获取 Job的 参数信息 JobParameters
                OutColour.out.printlnRed("Hello SpringBatch web 调用....Job的全部参数：" + jobParameters);
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Step step005(){
        TaskletStep step005 = stepBuilderFactory.get("step005")
                .tasklet(tasklet005())
                .build();
        return step005;
    }
    //定义作业
    @Bean
    public Job job005(){
        //如果 ，没有加 .incrementer(new RunIdIncrementer())  会找不到 get("job005") 的 job005 值，... TODO xxx
        Job job = jobBuilderFactory.get("job005")//No job parameters incrementer found for job=job
                .start(step005())
                .incrementer(new RunIdIncrementer())
                .build();
        return job;
    }
}
